package com.example.jiagoujiexiang.Controller;

import com.example.jiagoujiexiang.Exption.MD5Util;
import com.example.jiagoujiexiang.Service.adminService;
import com.example.jiagoujiexiang.Service.petService;
import com.example.jiagoujiexiang.Service.subService;
import com.example.jiagoujiexiang.Service.userService;
import com.example.jiagoujiexiang.entity.Pet;
import com.example.jiagoujiexiang.entity.User;
import com.example.jiagoujiexiang.entity.subscribe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class adminController {
    @Autowired
    adminService adminService;
    @Autowired
    userService userService;
    @Autowired
    petService petService;
    @Autowired
    subService subService;

    @GetMapping("/admin")
    public String adminPage(Model model) {
        model.addAttribute("contentSrc", "useradmin");
        return "admin";
    }

    @GetMapping("/useradmin")
    public String userAdmin(Model model) {
        List<User> userList = adminService.getUserListS();
        model.addAttribute("userList", userList);
        return "useradmin";
    }
    @GetMapping("/Petadmin")
    public String Petadmin(Model model) {
        List<Pet> petList = petService.getmoredogs();
        petList.addAll(petService.getmorecats());
        model.addAttribute("petList",petList);
        return "adminPet";
    }

    @GetMapping("/subadmin")
    public String subAdmin(Model model) {
        List<subscribe> SubscribeList = adminService.getcatSubscribeListS();
        SubscribeList.addAll(adminService.getdogSubscribeListS());
        for (subscribe subscribe : SubscribeList) {
            subscribe.setUser(userService.getUserByid(subscribe.getUser_id()));
            if (petService.getdogsde(subscribe.getPet_id()) != null) {
                subscribe.setPet(petService.getdogsde(subscribe.getPet_id()));
            } else {
                subscribe.setPet(petService.getcatsde(subscribe.getPet_id()));
            }
        }
        model.addAttribute("SubscribeList", SubscribeList);
        return "subadmin";
    }

    @GetMapping("/deleteSub")
    public String deleteSub(@RequestParam(name = "pet_id") int pet_id, Model model) {
        subService.deleSubscribe(pet_id);
        model.addAttribute("message", "删除成功");
        return "redirect:/subadmin";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(@RequestParam(name = "user_id") int user_id, Model model) {
        userService.deleteUser(user_id);
        model.addAttribute("message", "删除成功");
        return "redirect:/useradmin";
    }

    @PostMapping("/upSubdata")
    public String updateSubscribe(@ModelAttribute subscribe subscribe, RedirectAttributes redirectAttributes) {
        subService.updateSubscribe(subscribe);
        redirectAttributes.addFlashAttribute("message", "修改成功");
        return "redirect:/subadmin";
    }
    @PostMapping("/upUserdata")
    public String upUserdata(@ModelAttribute User user, RedirectAttributes redirectAttributes) {
        String MD5password = MD5Util.md5(user.getPassword());
        user.setPasswordSecret(MD5password);
        userService.updateUseradmin(user);
        redirectAttributes.addFlashAttribute("message", "修改成功");
        return "redirect:/useradmin";
    }
}
